<?php
/**
 * Interface for all the scopeable binders to implement
 */
abstract class IOC_ScopeableBinder implements IOC_Binder {
    private $scope = null;
    private $instances = array();
    
    /**
     * Indicates the scope in which we want the instance to live 
     * @param $scope
     */
    public function in($scope) {
        if(empty($scope)) {
            throw new IOC_BindingException("Invalid scope.");
        }
        $this->scope = $scope;
    }
    /**
     * Gets an instance of the classname
     * @param $className
     * @return object
     */
	public function get($className) {
	    if(is_null($this->scope)) {
	        //there is no scope, always return a new instance
	        return $this->build($className);
	    } elseif($this->scope==IOC_Scope::SINGLE_PER_REQUEST) {
	        if(!isset($this->instances[$className])) {
	            $this->instances[$className]=$this->build($className);
	        }
	        return $this->instances[$className];
	    }
	}
	
	/**
	 * Builds an Instance of the specified class or the class specified on construction
	 * time
	 * @param $className
	 * @return object
	 */
	protected abstract function build($className);
}
?>